package com.example.administrator.hqq.adapter.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.hqq.R;
import com.example.administrator.hqq.adapter.SetOnClickItem;
import com.example.administrator.hqq.bean.DIalogContactBean;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/30 10:56
 * @remark:
 */
public class DIalogContactAdapter extends RecyclerView.Adapter<DIalogContactAdapter.ViewHoler> {
    private List<DIalogContactBean.DataBean> data;
    private Context context;
    private OnClickItem onClickItem;
    public DIalogContactAdapter(List<DIalogContactBean.DataBean> data, Context context) {
        this.data = data;
        this.context = context;
    }
    @NonNull
    @Override
    public ViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.contact_item, null);
        List<DIalogContactBean.DataBean.ContactBean> contact = new ArrayList<>();
        ContactItemAdapter contactItemAdapter = new ContactItemAdapter(context,contact);
        ViewHoler viewHoler = new ViewHoler(inflate,contactItemAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        viewHoler.child.setLayoutManager(linearLayoutManager);
        viewHoler.child.setAdapter(contactItemAdapter);
        viewHoler.setList(contact);
        return viewHoler;
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHoler holder, final int position) {
        holder.child.setVisibility(View.GONE);
        holder.view.setVisibility(View.GONE);
        holder.shrinkButtonTrue.setVisibility(View.GONE);
        holder.shrinkButtonFalse.setVisibility(View.GONE);
        if (data.get(position).isIs_show()){
            holder.child.setVisibility(View.VISIBLE);
            holder.shrinkButtonTrue.setVisibility(View.VISIBLE);
            holder.view.setVisibility(View.VISIBLE);
            holder.list.clear();
            holder.list.addAll(data.get(position).getContact());
            holder.contactItemAdapter.notifyDataSetChanged();
        }
        else{
            holder.shrinkButtonFalse.setVisibility(View.VISIBLE);
        }
        holder.contactItemAdapter.setSetOnClickItem(new SetOnClickItem() {
            @Override
            public void setOnClickItem(int position1) {
                if (onClickItem!=null)
                {
                    onClickItem.onClickeChild(position,position1);
                }
            }

            @Override
            public void setPicsOnClickItem(int position1) {
                if (onClickItem!=null)
                {
                    onClickItem.onLongClickeChild(position,position1);
                }
            }
        });
        holder.auto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickItem!=null)
                {
                    onClickItem.onClikeItem(position);
                }
            }
        });
        holder.groupTitle.setText(data.get(position).getTopic_name());
        holder.groupNumber.setText(data.get(position).getContact().size()+"");
    }

    public void setOnClickItem(OnClickItem onClickItem) {
        this.onClickItem = onClickItem;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class ViewHoler extends RecyclerView.ViewHolder {
        @BindView(R.id.shrink_button_false)
        ImageView shrinkButtonFalse;
        @BindView(R.id.shrink_button_true)
        ImageView shrinkButtonTrue;
        @BindView(R.id.group_title)
        TextView groupTitle;
        @BindView(R.id.group_number)
        TextView groupNumber;
        @BindView(R.id.child)
        RecyclerView child;
        @BindView(R.id.auto)
        AutoLinearLayout auto;
        @BindView(R.id.view)
        View view;

        ContactItemAdapter contactItemAdapter;
        private List list;

        public void setList(List list) {
            this.list = list;
        }

        public ViewHoler(View itemView, ContactItemAdapter contactItemAdapter) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.contactItemAdapter=contactItemAdapter;
        }
    }
   public interface OnClickItem {
        void onClikeItem(int position);
        void onClickeChild(int position,int childpositing);
        void onLongClickeChild(int position,int childpositing);
   }
}
